package mainpk;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

public class FormAluno extends Form implements CommandListener{

	public static final int NEW = 0;
	public static final int EDIT = 1;
	public static final int VIEW = 2;
	
	private int mode;
	private Animation midlet;
	private Command cmSave, cmBack;
	private TextField tfNumero, tfNome, tfNota;
	
	private Aluno aluno;
	private int index;
	
	public FormAluno(String title, Animation midlet) {
		super(title);
		this.midlet = midlet;

		this.cmBack = new Command("Back", Command.BACK, 0);
		this.cmSave = new Command("Save", Command.OK, 0);

		this.tfNumero = new TextField("Numero", "", 2, TextField.NUMERIC);
		this.tfNome = new TextField("Nome", "", 10, TextField.ANY);
		this.tfNota = new TextField("Nota", "", 4, TextField.DECIMAL);

		this.addCommand(this.cmBack);
		
		this.append(tfNumero);
		this.append(tfNome);
		this.append(tfNota);

		this.setCommandListener(this);
	}

	public void commandAction(Command c, Displayable d) {
		
		if (c == cmSave){
			if (this.mode == NEW) {
				Aluno a = new Aluno(
						Integer.parseInt(this.tfNumero.getString()),
						this.tfNome.getString(), Double.parseDouble(this.tfNota
								.getString()));

				this.midlet.disc.add(a);
			}
			if (this.mode == EDIT) {
				
				
			}
		}
		
		
		this.midlet.displayMgr.popDisplayable();
	}

	public void setMode(int mode, int index) {
		this.mode = mode;
		if (mode == NEW) {
			this.tfNumero.setString("");
			this.tfNome.setString("");
			this.tfNota.setString("");
			this.removeCommand(this.cmSave);

			this.addCommand(this.cmSave);

		}
		if (mode == EDIT) {
			this.aluno = new Aluno();
			this.index = index;
			int id = this.midlet.disc.getIdFromIndex(index);
			this.midlet.disc.read(id, this.aluno);
			
			this.tfNumero.setString(this.aluno.getNumero() + "");
			this.tfNome.setString(this.aluno.getName());
			this.tfNota.setString(this.aluno.getNota() + "");
			
			this.removeCommand(this.cmSave);

			this.addCommand(this.cmSave);

		}
		
		if (mode == VIEW){
			this.aluno = new Aluno();
			this.index = index;
			int id = this.midlet.disc.getIdFromIndex(index);
			this.midlet.disc.read(id, this.aluno);
			
			
			this.tfNumero.setString(aluno.getNumero() + "");
			this.tfNome.setString(aluno.getName());
			this.tfNota.setString(aluno.getNota() + "");
			
			this.removeCommand(this.cmSave);
	
		}
	}
	
	
	
	
	

}
